<?php
namespace Common\Controller;
use Think\Controller;

/**
 * Movie Data Model Proxy Class
 *
 * @package Common\Controller
 * @author lythesia
**/
class MovieController extends Controller {

// {{{ shared functions
  /**
   * Get Movie data by `movie_id`
   *
   * @param  string movid_id
   * @param  string fields(optional)
   * @return array query result
   * @author lythesia
   **/
  public function getMovieById($id, $field='') {
    $Movie = M('Movie');
    $data = $Movie->field($field)->find($id);
    return $data;
  }

  /**
   * Get Movie and related data by `movie_id`
   *
   * @param  string movie_id
   * @param  array  options
   * @param  string fields(optional)
   * @return array query result
   * @author lythesia
   **/
  public function getMovieRelById($id, $opt, $field='') {
    $Movie = D('Movie');
    $data = $Movie->field($field)->relation($opt['rel'])->find($id);
    return $data;
  }


  /**
   * Get Movie data sets by conditions
   *
   * @param  array query conditions
   * @param  array options
   * @param  string fields(optional)
   * @param  boolean if need count query results(optional, default false)
   * @return array query result
   * @author lythesia
   **/
  public function getMoviesByCond($cond, $opt, $field='', $if_count = false) {
    $Movie = D('Movie');
    $where = parse_complex_cond($cond);
    
    $data = $Movie->field($field)->limit($opt['offset'], $opt['size'])->order($opt['order'])->relation($opt['rel'])->where($where)->select();

    if($if_count) {
      $count = $Movie->where($where)->count();
      return array($count, $data);
    }
    else return $data;
  }
// }}}


// {{{ inter-module functions
  /**
   * Count movies by conditions
   *
   * @param  array query condtions(optional, default empty)
   * @return int query count
   * @author lythesia
   **/
  public function countMovies($where='') {
    $Movie = M('Movie');
    $count = $Movie->where($where)->count();
  }

  /**
   * Get top & recent movies
   *
   * @param  array options
   * @param  string fields(optional)
   * @return array query result
   * @author lythesia
   **/
  public function getTopNewMovies($opt, $field='') {
    $Movie = D('Movie');
    $data = $Movie->field($field)->page($opt['page'], $opt['count'])->order($opt['order'])->relation($opt['rel'])->select();
    return $data;
  }

// }}}

// {{{ test
// }}}  
}
